package collectionsUtils;

import org.junit.Test;
import pojo.User;

import java.util.*;

public class MyUnionSetCoinCombinationMethod {

    /**
     * 功能描述: 一个元素是否在集合中
     */
    @Test
    public void testTwoList(){
        int userSize =10;
        List<User> userList = generateUserList(userSize);
        MyUnionSet.UnionSet<User> unionSet = new MyUnionSet.UnionSet<>(userList);
        for (int i = 0; i < userList.size()-1; i++) {
            unionSet.union(userList.get(i),userList.get(i+1));
        }

        for (int i = 0; i < userList.size(); i++) {
            System.out.println(unionSet.isSameSet(userList.get(i),
                                                  new User("2",
                                                           "3")));
        }
    }


    /**
     * 功能描述: 两个集合内的元素的问题
     */
    @Test
    public void testTwoList2(){
        int userSize =10;
        List<User> userList = generateUserList(userSize);
        List<User> userList2 = generateUserList(userSize);
        MyUnionSet.UnionSet<User> unionSet = new MyUnionSet.UnionSet<>(userList);
        for (int i = 0; i < userList.size()-1; i++) {
            unionSet.union(userList.get(i),userList.get(i+1));
        }

        List<User> isSameList = new ArrayList<>();
        List<User> isDiffList = new ArrayList<>();
        for (int index = 0; index < userList2.size(); index++) {


            if (unionSet.isSameSet(userList.get(0),userList2.get(index))){
                isSameList.add(userList2.get(index));
            }else {
                isDiffList.add(userList2.get(index));
            }
        }

        System.out.println("userList:"+userList);
        System.out.println("userList2:"+userList2);
        System.out.println("isSameList:"+isSameList);
        System.out.println("isDiffList:"+isDiffList);
        System.out.println(isDiffList.size() + isSameList.size());
    }

    /**
     * 功能描述: 用户去重,如果有重复的,则合并
     */
    // 重复的逻辑: 1.userId一致, 2.userName一致 3.nickName一致
    // 3哥属性,只要有一个属性相同,就认为是同一个用户,查询有多少个用户信息
    @Test
    public void testDistinctUser(){
        int userSize =5;
        List<User> userList = generateUserListAndNickName(userSize);
        MyUnionSet.UnionSet<User> unionSet = new MyUnionSet.UnionSet<>(userList);

        // 这三个的map的主要作用,就是存储userId,userName,userNickName 使进入 并查集的数据是不重复的,
        // 这样,在合并的时候,就不会出现重复的元素了
        Map<String,User> userIdMap = new HashMap<>();
        Map<String,User> userNameMap = new HashMap<>();
        Map<String,User> userNickNameMap = new HashMap<>();
        for (User user : userList) {
            // 如果userId相同,则合并,否则就是第一次出现,则直接放入map中
            if (userIdMap.containsKey(user.getUserId())){
                unionSet.union(userIdMap.get(user.getUserId()),user);
            }else {
                userIdMap.put(user.getUserId(),user);
            }
            // 如果userName相同,则合并,否则就是第一次出现,则直接放入map中
            if (userNameMap.containsKey(user.getUserName())){
                unionSet.union(userNameMap.get(user.getUserName()),user);
            }else {
                userNameMap.put(user.getUserName(),user);
            }
            // 如果nickName相同,则合并,否则就是第一次出现,则直接放入map中
            if (userNickNameMap.containsKey(user.getNickName())){
                unionSet.union(userNickNameMap.get(user.getNickName()),user);
            }else {
                userNickNameMap.put(user.getNickName(),user);
            }
        }

        System.out.println("用户信息的个数为"+unionSet.getSize());
        userList.forEach(System.out::println);
    }
    private List<User> generateUserList(int userSize) {
        Random random = new Random(userSize);
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < userSize; i++) {

            userList.add(new User(String.valueOf(getRandomInt(userSize)), String.valueOf(getRandomInt(userSize))));
        }
        return userList;
    }

    private List<User> generateUserListAndNickName(int userSize) {
        Random random = new Random(userSize);
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < userSize; i++) {

            userList.add(new User(String.valueOf(getRandomInt(userSize)), String.valueOf(getRandomInt(userSize)),String.valueOf(getRandomInt(userSize))));
        }
        return userList;
    }
    public int getRandomInt(int max){
        return (int)(Math.random()*max);
    }
}
